package com.zhangyu.shadow.ec.main.cart;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.tepth.shadow.ui.recycler.DataConverter;
import com.tepth.shadow.ui.recycler.MultipleField;
import com.tepth.shadow.ui.recycler.MultipleItemEntity;

import java.util.ArrayList;

/**
 * @author zhangyu
 * @date 2017/11/15
 */

public class ShopCartDataConverter extends DataConverter {
    @Override
    public ArrayList<MultipleItemEntity> convert() {
        ArrayList<MultipleItemEntity> dataList = new ArrayList<>();
        final JSONArray jsonArray = JSON.parseObject(getJsonData()).getJSONArray("data");
        int size = jsonArray.size();
        for (int i = 0; i < size; i++) {
            final JSONObject jsonObject = jsonArray.getJSONObject(i);
            final int id = jsonObject.getInteger("id");
            final String title = jsonObject.getString("title");
            final String desc = jsonObject.getString("desc");
            final double price = jsonObject.getDouble("price");
            final int count = jsonObject.getInteger("count");
            final String thumb = jsonObject.getString("thumb");

            final MultipleItemEntity entity = new MultipleItemEntity.MultipleItemEntityBuilder()
                    .setItemType(ShopCartItemType.SHOP_CART_ITEM)
                    .setField(MultipleField.ID, id)
                    .setField(MultipleField.IMAGE_URL, thumb)
                    .setField(ShopCartItemFields.TITLE, title)
                    .setField(ShopCartItemFields.DESC, desc)
                    .setField(ShopCartItemFields.PRICE, price)
                    .setField(ShopCartItemFields.COUNT, count)
                    .setField(ShopCartItemFields.IS_SELECT,true)
                    .build();
            dataList.add(entity);
        }
        return dataList;
    }
}
